home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
UTILITY1
/
MSWSRC35.ZIP
/
DIB.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-02
|
14KB
|
456 lines
//-----------------------------------------------------------------------------
// DIB.C
//
// This is a collection of useful DIB manipulation/information gathering
// functions. Many functions are supplied simply to take the burden
// of taking into account whether a DIB is a Win30 style or OS/2 style
// DIB away from the application.
//
// The functions in this module assume that the DIB pointers or handles
// passed to them point to a block of memory in one of two formats:
//
// a) BITMAPINFOHEADER + color table + DIB bits (3.0 style DIB)
// b) BITMAPCOREHEADER + color table + DIB bits (OS/2 PM style)
//
// The SDK Reference, Volume 2 describes these data structures.
//
// A number of functions in this module were lifted from SHOWDIB,
// and modified to handle OS/2 DIBs.
//
// The functions in this module could be streamlined (made faster and
// smaller) by removing the OS/2 DIB specific code, and assuming all
// DIBs passed to it are Win30 style DIBs. The DIB file reading code
// would need to be modified to always convert DIBs to Win30 style
// DIBs. The only reason this isn't done in DIBView is because DIBView
// was written to test display and printer drivers (which are supposed
// to support OS/2 DIBs wherever they support Win30 style DIBs). SHOWDIB
// is a great example of how to go about doing this.
//-----------------------------------------------------------------------------
#include "allwind.h"
#include "dib.h"
#define PALVERSION 0x300
extern LPSTR FindDIBBits (LPSTR lpbi);
extern WORD DIBNumColors (LPSTR lpbi);
extern WORD PaletteSize (LPSTR lpbi);
extern DWORD DIBHeight (LPSTR lpDIB);
extern DWORD DIBWidth (LPSTR lpDIB);
extern HBITMAP DIBToBitmap (HANDLE hDIB, HPALETTE hPal);
extern void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr,DWORD dwWidth,DWORD dwHeight,int nBPP);
extern HANDLE BitmapToDIB (HBITMAP hBitmap, HPALETTE hPal);
//---------------------------------------------------------------------
//
// Function: FindDIBBits
//
// Purpose: Given a pointer to a DIB, returns a pointer to the
// DIB's bitmap bits.
//
// Parms: lpbi == pointer to DIB header (either BITMAPINFOHEADER
// or BITMAPCOREHEADER)
//
// History: Date Reason
// 6/01/91 Created
//
//---------------------------------------------------------------------
LPSTR FindDIBBits (LPSTR lpbi)
{
return (lpbi + *(LPDWORD)lpbi + PaletteSize (lpbi));
}
//---------------------------------------------------------------------
//
// Function: DIBNumColors
//
// Purpose: Given a pointer to a DIB, returns a number of colors in
// the DIB's color table.
//
// Parms: lpbi == pointer to DIB header (either BITMAPINFOHEADER
// or BITMAPCOREHEADER)
//
// History: Date Reason
// 6/01/91 Created
//
//---------------------------------------------------------------------
WORD DIBNumColors (LPSTR lpbi)
{
WORD wBitCount;
// If this is a Windows style DIB, the number of colors in the
// color table can be less than the number of bits per pixel
// allows for (i.e. lpbi->biClrUsed can be set to some value).
// If this is the case, return the appropriate value.
if (IS_WIN30_DIB (lpbi))
{
DWORD dwClrUsed;
dwClrUsed = ((LPBITMAPINFOHEADER) lpbi)->biClrUsed;
if (dwClrUsed)
return (WORD) dwClrUsed;
}
// Calculate the number of colors in the color table based on
// the number of bits per pixel for the DIB.
if (IS_WIN30_DIB (lpbi))
wBitCount = ((LPBITMAPINFOHEADER) lpbi)->biBitCount;
else
wBitCount = ((LPBITMAPCOREHEADER) lpbi)->bcBitCount;
switch (wBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
//---------------------------------------------------------------------
//
// Function: PaletteSize
//
// Purpose: Given a pointer to a DIB, returns number of bytes
// in the DIB's color table.
//
// Parms: lpbi == pointer to DIB header (either BITMAPINFOHEADER
// or BITMAPCOREHEADER)
//
// History: Date Reason
// 6/01/91 Created
//
//---------------------------------------------------------------------
WORD PaletteSize (LPSTR lpbi)
{
if (IS_WIN30_DIB (lpbi))
return (DIBNumColors (lpbi) * sizeof (RGBQUAD));
else
return (DIBNumColors (lpbi) * sizeof (RGBTRIPLE));
}
//---------------------------------------------------------------------
//
// Function: DIBHeight
//
// Purpose: Given a pointer to a DIB, returns its height. Note
// that it returns a DWORD (since a Win30 DIB can have
// a DWORD in its height field), but under Win30, the
// high order word isn't used!
//
// Parms: lpDIB == pointer to DIB header (either BITMAPINFOHEADER
// or BITMAPCOREHEADER)
//
// History: Date Reason
// 6/01/91 Created
//
//---------------------------------------------------------------------
DWORD DIBHeight (LPSTR lpDIB)
{
LPBITMAPINFOHEADER lpbmi;
LPBITMAPCOREHEADER lpbmc;
lpbmi = (LPBITMAPINFOHEADER) lpDIB;
lpbmc = (LPBITMAPCOREHEADER) lpDIB;
if (lpbmi->biSize == sizeof (BITMAPINFOHEADER))
return lpbmi->biHeight;
else
return (DWORD) lpbmc->bcHeight;
}
//---------------------------------------------------------------------
//
// Function: DIBWidth
//
// Purpose: Given a pointer to a DIB, returns its width. Note
// that it returns a DWORD (since a Win30 DIB can have
// a DWORD in its width field), but under Win30, the
// high order word isn't used!
//
// Parms: lpDIB == pointer to DIB header (either BITMAPINFOHEADER
// or BITMAPCOREHEADER)
//
// History: Date Reason
// 6/01/91 Created
//
//---------------------------------------------------------------------
DWORD DIBWidth (LPSTR lpDIB)
{
LPBITMAPINFOHEADER lpbmi;
LPBITMAPCOREHEADER lpbmc;
lpbmi = (LPBITMAPINFOHEADER) lpDIB;
lpbmc = (LPBITMAPCOREHEADER) lpDIB;
if (lpbmi->biSize == sizeof (BITMAPINFOHEADER))
return lpbmi->biWidth;
else
return (DWORD) lpbmc->bcWidth;
}
//---------------------------------------------------------------------
//
// Function: DIBToBitmap
//
// Purpose: Given a handle to global memory with a DIB spec in it,
// and a palette, returns a device dependent bitmap. The
// The DDB will be rendered with the specified palette.
//
// Parms: hDIB == HANDLE to global memory containing a DIB spec
// (either BITMAPINFOHEADER or BITMAPCOREHEADER)
// hPal == Palette to render the DDB with. If it's NULL,
// use the default palette.
//
// History: Date Reason
// 6/01/91 Created
//
//---------------------------------------------------------------------
HBITMAP DIBToBitmap (HANDLE hDIB, HPALETTE hPal)
{
LPSTR lpDIBHdr, lpDIBBits;
HBITMAP hBitmap;
HDC hDC;
HPALETTE hOldPal = NULL;
if (!hDIB)
return NULL;
lpDIBHdr = (char *)GlobalLock (hDIB);
lpDIBBits = FindDIBBits (lpDIBHdr);
hDC = GetDC (NULL);
if (!hDC)
{
GlobalUnlock (hDIB);
return NULL;
}
if (hPal)
hOldPal = SelectPalette (hDC, hPal, FALSE);
RealizePalette (hDC);
hBitmap = CreateDIBitmap (hDC,
(LPBITMAPINFOHEADER) lpDIBHdr,
CBM_INIT,
lpDIBBits,
(LPBITMAPINFO) lpDIBHdr,
DIB_RGB_COLORS);
if (!hBitmap)
MessageBox(MainHWindow, "Not Enough Memory", "Error", M